﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace System
{
    public static class ObjectExtension
    {
        private static readonly Encoding _gb2312 = Encoding.GetEncoding("gb2312");


        public static T ConvertAs<T>(object obj, T defaultValue)
        {
            TypeConverter converter;
            T t = default(T);
            if (obj == null)
            {
                return defaultValue;
            }
            if (obj is T)
            {
                return (T)obj;
            }
            Type targetType = typeof(T);
            bool isToString = targetType == typeof(string);
            if (isToString)
            {
                converter = TypeDescriptor.GetConverter(obj.GetType());
            }
            else
            {
                converter = TypeDescriptor.GetConverter(targetType);
            }
            if (converter == null)
            {
                return defaultValue;
            }
            object targetObj = null;
            try
            {
                if (isToString)
                {
                    targetObj = converter.ConvertToString(obj);
                }
                else
                {
                    targetObj = converter.ConvertFromString(obj.ToString());
                }
                t = (T)targetObj;
            }
            catch
            {
            }
            return t;
        }
    
      
        public static int GetLengthForGB2312(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return 0;
            }
            return _gb2312.GetByteCount(str);
        }

        public static object GetPropertyValue(this object obj, string propertyName)
        {
            PropertyInfo pInfo = obj.GetType().GetProperty(propertyName);
            if (pInfo == null)
            {
                return null;
            }
            return pInfo.GetValue(obj, null);
        }

        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        public static string SerializeToString(this object obj)
        {
            string t;
            try
            {
                DataContractSerializer ser = new DataContractSerializer(obj.GetType());
                using (MemoryStream stream = new MemoryStream())
                {
                    XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
                    ser.WriteObject(writer, obj);
                    writer.Flush();
                    stream.Position = 0L;
                    t = new StreamReader(stream, Encoding.UTF8).ReadToEnd();
                }
            }
            catch
            {
                t = string.Empty;
            }
            return t;
        }

        public static XElement SerializeToXml(this object obj)
        {
            DataContractSerializer ser = new DataContractSerializer(obj.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
                ser.WriteObject(writer, obj);
                writer.Flush();
                stream.Position = 0L;
                XmlTextReader reader = new XmlTextReader(stream);
                return XElement.Load(reader);
            }
        }




      

        public static T To<T>(this object obj)
        {
            return ConvertAs<T>(obj, default(T));
        }

        public static T To<T>(this object obj, T defaultValue)
        {
            return ConvertAs<T>(obj, defaultValue);
        }

    


        public static string XToString(this object obj)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            return obj.ToString().Trim();
        }
    }
}
